home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 287_01 / fill.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-23  |  3.9 KB  |  172 lines

  1. #include <stdio.h>
  2. #include <gds.h>
  3.  
  4. struct rec1 {
  5.     int leftx, rightx, y, direction;
  6. };
  7.  
  8. typedef struct rec1 fillrec;
  9.  
  10. static int MAXQ;
  11. static int head, tail, qlen, empty, pattern;
  12. static fillrec *FQUE;
  13.  
  14. char *alloca();
  15.  
  16. SolidFill(x,y)
  17. int x,y;
  18. {
  19.     int leftx, rightx, dir;
  20.     unsigned int state;
  21.  
  22.     x+=ORGX;
  23.     y+=ORGY;
  24.     if (outside(x,y)) return;
  25.  
  26.     if ((state=stackavail()) < 64+16) {
  27.         graderror(4,9);
  28.     }
  29.     MAXQ=(state-64) >> 3;
  30.     FQUE=(fillrec *)alloca(MAXQ << 3);
  31.     head=tail=qlen=0;
  32.     switch (CUR_PLOT) {
  33.     case 2:
  34.         state=fr_read(calcaddr(x,y)) & DOTVALUE[x & 0x0f];
  35.         if (state) goto and_type;
  36.     case 0:
  37.         empty=0; pattern=0xffff; break;
  38.     case 1:
  39.     case 3:
  40. and_type:
  41.         empty=0xffff; pattern=0; break;
  42.     default:
  43.         graderror(10,10,__FILE__,__LINE__);
  44.     }
  45.     if (hlnlmt(x,y,empty,pattern,&leftx,&rightx)) return;
  46.     queue(leftx, rightx, y, 3);
  47.     while (qlen) {
  48.         dequeue(&leftx,&rightx,&y,&dir);
  49.         if (dir & 0x01) {
  50.             x=leftx;
  51.             do {
  52.                 x=filline(x,leftx,rightx,y-1,1,3);
  53.             } while (x<=rightx);
  54.         }
  55.         if (dir & 0x02) {
  56.             x=leftx;
  57.             do {
  58.                 x=filline(x,leftx,rightx,y+1,2,3);
  59.             } while (x<=rightx) ;
  60.         }
  61.     }
  62. }
  63.  
  64. queue(leftx,rightx,y,direction)
  65. int leftx,rightx,y,direction;
  66. {
  67.     if (y==WINY2) {
  68.         direction &= 0x01;
  69.     }
  70.     if (y==WINY1) {
  71.         direction &= 0x02;
  72.     }
  73.     if (direction==0) return;
  74.     if (qlen++ >= MAXQ) {
  75.         qlen--;
  76.         return;
  77.     }
  78.     FQUE[tail].leftx=leftx;
  79.     FQUE[tail].rightx=rightx;
  80.     FQUE[tail].y=y;
  81.     FQUE[tail].direction=direction;
  82.     if (++tail >= MAXQ)
  83.         tail=0;
  84. }
  85.  
  86. dequeue(leftx,rightx,y,direction)
  87. int *leftx, *rightx, *y, *direction;
  88. {
  89. /*    if (qlen <= 0) {
  90.         graderror(4,11,"Stack Underflow");
  91.     } */
  92.     qlen--;
  93.     *leftx=FQUE[head].leftx;
  94.     *rightx=FQUE[head].rightx;
  95.     *y=FQUE[head].y;
  96.     *direction=FQUE[head].direction;
  97.     if (++head >= MAXQ)
  98.         head=0;
  99. }
  100.  
  101. filline(x, leftx,rightx, y, cur_dir, allow_dir)
  102. int x, leftx, rightx, y, cur_dir, allow_dir;
  103. {
  104.     int newleftx, newrightx, newdir;
  105.  
  106.     x=skipright(x,y,~empty, rightx);
  107.     if (x <= rightx) {
  108.         hlnlmt(x,y,empty, pattern, &newleftx, &newrightx);
  109.         if ((cur_dir == allow_dir) ||
  110.                (newleftx < leftx) || (newrightx > rightx)) {
  111.             newdir=allow_dir;
  112.         } else {
  113.             newdir=cur_dir;
  114.         }
  115.         queue(newleftx, newrightx, y, newdir);
  116.         x=newrightx+2;
  117.     }
  118.     return(x);
  119. }
  120.  
  121. PatternFill(x,y,patptr,flag)
  122. int x,y, flag;
  123. int *patptr;
  124. {
  125.     int leftx, rightx, dir;
  126.     unsigned int state;
  127.  
  128.     x+=ORGX;
  129.     y+=ORGY;
  130.     if (outside(x,y)) return;
  131.  
  132.     if ((state=stackavail()) < 64+16) {
  133.         graderror(4,9);
  134.     }
  135.     MAXQ=(state-64) >> 3;
  136.     FQUE=(fillrec *)alloca(MAXQ << 3);
  137.     head=tail=qlen=0;
  138.     switch (CUR_PLOT) {
  139.     case 2:
  140.         state=fr_read(calcaddr(x,y)) & DOTVALUE[x & 0x0f];
  141.         if (state) goto and_type;
  142.     case 0:
  143.         empty=0; break;
  144.     case 1:
  145.     case 3:
  146. and_type:
  147.         pattern=0; break;
  148.     default:
  149.         graderror(10,10,__FILE__,__LINE__);
  150.     }
  151.     if (hlnlmt(x,y,empty,patptr[y & 0x0f],&leftx,&rightx)) return;
  152.     queue(leftx, rightx, y, 3);
  153.     while (qlen) {
  154.         dequeue(&leftx,&rightx,&y,&dir);
  155.         if (dir & 0x01) {
  156.             x=leftx;
  157.             do {
  158.                 pattern=patptr[(y-1) & 0x0f];
  159.                 x=filline(x,leftx,rightx,y-1,1,1);
  160.             } while (x<=rightx);
  161.         }
  162.         if (dir & 0x02) {
  163.             x=leftx;
  164.             do {
  165.                 pattern=patptr[(y+1) & 0x0f];
  166.                 x=filline(x,leftx,rightx,y+1,2,2);
  167.             } while (x<=rightx);
  168.         }
  169.     }
  170. }
  171.  
  172.